// 这里没用React.lazy，是因为它没法配合vite的热加载动态更新组件，所以替换成 '@loadable/component'，它可以
// import {lazy} from 'react';
//按需加载组件
import loadable from '@loadable/component';
import pMinDelay from 'p-min-delay'; //延迟加载页面

import Loading from '@/components/Loading';
import {
    LOGIN, NOTFOUND, HOME, CONFIGMANAGEMENT, SERVICECONFIGURATION, LIMTCONFIGURATION, REPORTSTATISTICS, SYSTEMMESSAGE
} from '@/config/url';
import {
    MailOutlined,
    PieChartOutlined,
    TeamOutlined,
    UserOutlined,
    ClockCircleOutlined,
    CodeSandboxOutlined
} from '@ant-design/icons';
import { useTranslation } from 'react-i18next';
const Login = loadable(() => import('@/pages/Login'), { fallback: <Loading /> });
const NotFound = loadable(() => import('@/pages/Forbidden'), { fallback: <Loading /> });
const Home = loadable(() => import('@/pages/Home'), { fallback: <Loading /> });
// const BillDetail = loadable(() => import('@/pages/BillDetail'), { fallback: <Loading /> });
const ConfigManagement = loadable(() => import('@/pages/ConfigManagement'), { fallback: <Loading /> });
const ServiceConfiguration = loadable(() => import('@/pages/ServiceConfiguration'), { fallback: <Loading /> });
const LimtConfiguration = loadable(() => import('@/pages/LimtConfiguration'), { fallback: <Loading /> });
const ReportStatistics = loadable(() => import('@/pages/ReportStatistics'), { fallback: <Loading /> });
const SystemMessage = loadable(() => import('@/pages/SystemMessage'), { fallback: <Loading /> });
export const routers = () => {
    const { t } = useTranslation();
    return [
        {
            url: LOGIN,
            icon: '',
            label: '欢迎登录',
            component: Login,
            showInMenu: false, // 是否展示在菜单上
            layout: null // 是否要布局结构
        },
        {
            url: NOTFOUND,
            icon: '',
            label: '404',
            component: NotFound,
            showInMenu: false,
        },
        {
            url: HOME,
            icon: <PieChartOutlined />,
            label: t('slider.information'),
            component: Home,
            showInMenu: true,
        },
        // {
        //     url: BILLDETAIL,
        //     icon: <PieChartOutlined />,
        //     label: 'User Information List',
        //     component: BillDetail,
        //     showInMenu: false,
        // },
        {
            url: CONFIGMANAGEMENT,
            icon: <CodeSandboxOutlined />,
            label: t('slider.configuration'),
            component: ConfigManagement,
            showInMenu: true,
        },
        {
            url: SERVICECONFIGURATION,
            icon: <MailOutlined />,
            label: t('slider.serviceConfiguration'),
            component: ServiceConfiguration,
            showInMenu: true,
        },
        {
            url: LIMTCONFIGURATION,
            icon: <TeamOutlined />,
            label:  t('slider.minimumConfiguration'),
            component: LimtConfiguration,
            showInMenu: true,
        },
        {
            url: REPORTSTATISTICS,
            icon: <UserOutlined />,
            label:  t('slider.platform'),
            component: ReportStatistics,
            showInMenu: true,
        },
        {
            url: SYSTEMMESSAGE,
            icon: <MailOutlined />,
            label:  t('slider.systemMessages'),
            component: SystemMessage,
            showInMenu: true,
        },
        // {
        //     url: 'test',
        //     icon: '',
        //     label: '测试',
        //     // component: ,
        //     showInMenu: true,
        //     children: [
        //         {
        //             url: HOME,
        //             icon: '',
        //             label: '测试case',
        //             component: Case, 
        //             showInMenu: true,
        //             // children: [{
        //             //     url: ADMIN,
        //             //     icon: '',
        //             //     label: '用户列表详情',
        //             //     component: Case,
        //             //     showInMenu: true,
        //             // }, {
        //             //     url: AGREEMENT,
        //             //     icon: '',
        //             //     label: '用户列表详情',
        //             //     component: User,
        //             //     showInMenu: true,
        //             // }
        //             // ]
        //         },
        //         {
        //             url: REFERE,
        //             icon: '',
        //             label: '测试用例',
        //             component: Refere,
        //             showInMenu: true,
        //         },
        //     ]
        // },
        // {
        //     url: 'system',
        //     icon: '',
        //     label: '仪表盘',
        //     // component: ,
        //     showInMenu: true,
        //     // children: [
        //     //     {
        //     //         url: HOME,
        //     //         icon: '',
        //     //         label: '用户列表',
        //     //         component: User,
        //     //         showInMenu: true,
        //     //     },
        //     //     // {
        //     //     //     url: ROLE,
        //     //     //     icon: '',
        //     //     //     label: '权限管理',
        //     //     //     component: Role,
        //     //     //     showInMenu: true,
        //     //     // },
        //     // ]
        // },

    ]
}

/**
 *获取有权限的路由
 *
 * @param {*} [routers=[]]
 * routers: {
 *      code: 'xxx-xxx-xxx', // 根据code映射组件路径permission-manage
 *      children: [], // 子路由
 *      icon: '', // 页面菜单的icon
 *      label: '', // 页面菜单的名称
 *      layout: '', // 页面的布局名称
 *      showInMenu: '', // 是否展示在菜单上
 *      rest, // 其他数据
 * }
 * @return {*} 一维路由数组
 */
export const addDynamicRoute = (routers = []) => {
    const pages = import.meta.glob('@/pages/*/index.jsx');
    return routers.map(item => {
        const params = item.code.split('-'),
            pageUrl = params.join('/'),
            filePath = params.map(i => i.charAt(0).toUpperCase() + i.slice(1)).join('/'),
            path = `/src/pages/${filePath}/index.jsx`,
            component = pages[path] ? loadable(pages[path], { fallback: <Loading /> }) : <Loading tip="配置与资源不符，请联系技术同学处理" />;

        if (Array.isArray(item.children) && item.children.length) {
            item.children = addDynamicRoute(item.children);
        }
        return (
            {
                url: `${BASEDIR}/${pageUrl || ''}`,
                icon: item.icon || '',
                label: item.name || '',
                component,
                layout: null,
                showInMenu: false,
            }
        );
    })
};
